﻿
@{
    ViewData["Title"] = "Index";
}

<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 作品管理 <span class="c-gray en">&gt;</span> 作品展示 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <form class="ajaxTable" data-target="table">
        <div class="text-c">
            日期范围：
            <input type="text" name="startDate" onfocus="WdatePicker({ maxDate: '#F{$dp.$D(\'logmax\')||\'%y-%M-%d\'}' })" id="logmin" class="input-text Wdate" style="width: 120px;">
            -
            <input type="text" name="endDate" onfocus="WdatePicker({ minDate: '#F{$dp.$D(\'logmin\')}', maxDate: '%y-%M-%d' })" id="logmax" class="input-text Wdate" style="width: 120px;">
            <input type="text" name="title" id="" placeholder=" 名称" style="width: 250px" class="input-text">
            <button name="" id="" class="btn btn-success" type="submit"><i class="Hui-iconfont">&#xe665;</i> 搜索</button>
        </div>
    </form>
    <div class="layui-btn-group demoTable">
        <span class="l">
            <button class="layui-btn" data-type="getCheckLength">下载选中</button>
            @*<button class="layui-btn" data-type="isAll">下载全部</button>*@
        </span>
    </div>
    <div class="mt-20">
        <table class="layui-table" lay-data="{url: '@Url.Action("DataJson", "Product")',height:'full-320',id:'table',page: true,limits: [10, 20,50]}" lay-filter="table" id="table">
            <thead>
                <tr>
                    <th lay-data="{type:'checkbox', fixed: 'left'}"></th>
                    <th lay-data="{field:'id', width:80, sort: true}">ID</th>
                    <th lay-data="{field:'title', width:580}">标题</th>
                    <th lay-data="{field:'annex', width:580}">下载地址</th>
                    <th lay-data="{field:'addTime', sort: true}">添加时间</th>
                    <th lay-data="{field:'status', templet: '#checkboxTpl', unresize: true}">状态</th>
                    <th lay-data="{field:'operation',fixed: 'right', width:150,sort: false, toolbar: '#barDemo'}">操作</th>
                </tr>
            </thead>
        </table>
        <script type="text/html" id="checkboxTpl">
            <input type="checkbox" name="lock" data-id="{{d.id}}" value="{{d.status}}" title="有效" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="details">查看</a>
            @*<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>*@
        </script>
    </div>
</div>
@section Scripts{
    <script type="text/javascript">
        //引用lay-ui的table控件
        layui.use('table', function () {
            var table = layui.table
                ,form = layui.form;

            var active = {
                getCheckLength: function() { //导出选中
                    var checkStatus = table.checkStatus('table'), data = checkStatus.data;
                    if (data.length > 0) {
                        for (var index = 0; index < data.length; index++) {
                            console.log(data[index].annex);
                            var urlArr = data[index].annex.split('/');
                            download(urlArr[urlArr.length-1], data[index].annex);
                        }
                    }
                }

            };

            //监听工具条
            table.on('tool(table)', function(obj){
                var data = obj.data;
                if (obj.event == 'edit'){
                    var url = '@Url.Action("Edit", "Product")/' + data.id;
                    layer_full("编辑", url);
                } else if (obj.event == 'details'){
                    var url = '@Url.Action("Details", "Product")/' + data.id;
                    layer_full("查看", url);
                } else if (obj.event == 'del') {
                    layer.confirm('确认要删除吗？', function(index) {
                        ajaxAntiForgery('@Url.Action("Delete", "Product")', { Id: data.id });
                    });

                }
            });

            //监听锁定操作
            form.on('checkbox(lockDemo)', function (obj) {
                ajaxAntiForgery('@Url.Action("Lock", "Product")', { Id: $(this).data("id"), Checked: obj.elem.checked });
            });

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });


        function download(name, href) {
            console.log(href);
            var a = document.createElement("a"), //创建a标签
                e = document.createEvent("MouseEvents"); //创建鼠标事件对象
            e.initEvent("click", false, false); //初始化事件对象
            a.href = "http://129.28.66.32:7002" + href; //设置下载地址
            a.download = name; //设置下载文件名
            a.dispatchEvent(e); //给指定的元素，执行事件click事件
        }
    </script>

}